package training_camp.day6.leetcode383;

import java.util.HashMap;
import java.util.Set;

class Solution {
    /**
     * 给你两个字符串：ransomNote 和 magazine ，判断 ransomNote 能不能由 magazine 里面的字符构成。
     *
     * @param ransomNote
     * @param magazine
     * @return
     */
    public boolean canConstruct(String ransomNote, String magazine) {
        HashMap<Character, Integer> rMap = new HashMap<>();
        HashMap<Character, Integer> mMap = new HashMap<>();
        char[] rChars = ransomNote.toCharArray();
        char[] mChars = magazine.toCharArray();
        for (char c : rChars) {
            rMap.put(c, rMap.getOrDefault(c, 0) + 1);
        }
        for (char c : mChars) {
            mMap.put(c, mMap.getOrDefault(c, 0) + 1);
        }
        Set<Character> set = rMap.keySet();
        for (char c : set) {
            if (!mMap.containsKey(c)) return false;
            if (rMap.get(c) > mMap.get(c)) return false;
        }
        return true;
    }
}